package com.kun.index.controller;


import com.kun.common.Constants;
import com.kun.common.ResultDto;
import com.kun.common.annotation.PermissionAuthentication;
import com.kun.common.exception.MyException;
import com.kun.index.controller.dto.LoginDto;
import com.kun.index.entity.Blog;
import com.kun.index.service.impl.BlogServiceImpl;
import com.kun.index.service.impl.UserServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 博客表 前端控制器
 * </p>
 *
 * @author kunkun
 * @since 2024-02-28
 */
@RestController
@RequestMapping("/index/blog")
public class BlogController {

    @Autowired
    private BlogServiceImpl blogService;

    @GetMapping("/queryBlogById")
    @ApiOperation("通过id查询博客信息")
    @PermissionAuthentication
    public ResultDto queryBlogById(@RequestParam(name = "id", required=true)Integer id){
        Blog blog = blogService.queryBlogById(id);
        if (blog!=null){
            return ResultDto.success(blog);
        }else{
            return ResultDto.fail(Constants.CODE_302,"数据查询不到,请重新查找数据");
        }


    }

    @GetMapping("/queryAllBlogs")
    @ApiOperation("查询所有的blog")
    @PermissionAuthentication
    public ResultDto queryAllBlogs(@RequestParam(name = "pageIndex", required=false)Integer pageIndex,
                                   @RequestParam(name = "pageSize", required=false)Integer pageSize,
                                   @RequestParam(name = "queryTitleLike", required=false)String queryTitleLike) {
        HashMap<String, Object> map = new HashMap<>();
        if (pageIndex != null) {
            map.put("pageIndex", pageIndex);
        }
        if (pageSize != null) {
            map.put("pageSize", pageSize);
        }
        if (queryTitleLike != null) {
            map.put("queryTitleLike", queryTitleLike);
        }
        Map<String, Object> resultMap = blogService.queryBlogs(map);
        if (((List<Blog>) resultMap.get("blogs")).size() > 0) {
            return ResultDto.success(resultMap);
        } else {
            return ResultDto.fail(Constants.CODE_301, "数据为空,请重新添加数据");
        }


    }

        @PostMapping("/addBlog")
    @ApiOperation("新增博客")
    public ResultDto addBlog(@RequestBody Blog blog){
        int num = blogService.addBlog(blog);
        if (num>0){
            return ResultDto.success("新增博客成功");
        }else{
            return ResultDto.fail("新增博客失败");
        }
    }

    @PostMapping("/updateBlog")
    @ApiOperation("编辑博客")
    public ResultDto updateBlog(@RequestBody Blog blog){
        int num = blogService.updateBlog(blog);
        if (num>0){
            return ResultDto.success("修改博客成功");
        }else{
            return ResultDto.fail("修改博客失败");
        }


    }

    @PostMapping("/deleteBlog")
    @ApiOperation("删除博客")
    public ResultDto deleteBlog(@RequestBody Blog blog){
        int num = blogService.deleteBlog(blog);
        if (num>0){
            return ResultDto.success("删除博客成功");
        }else{
            return ResultDto.fail("删除博客失败");
        }


    }
    @PostMapping("/batchDeleteBlog")
    @ApiOperation("批量删除博客")
    public ResultDto batchDeleteBlog(@RequestBody List<Integer> ids){
        int num = blogService.batchDeleteBlog(ids);
        if (num>0){
            return ResultDto.success("删除博客成功");
        }else{
            return ResultDto.fail("删除博客失败");
        }


    }


}

